package com.showde.shoppingmall.adapter;

import java.util.ArrayList;
import java.util.List;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.domain.Product;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductListAdapter extends BaseAdapter {

	private Context context;
	private List<Product> products;
	ImageLoader imageLoader;

	public ProductListAdapter(Context context, List<Product> products) {
		this.context = context;
		this.products = products;
		if (this.products == null) {
			this.products = new ArrayList<Product>();
		}
		imageLoader = new ImageLoader(context);
	}

	public void resetData(List<Product> products) {
		this.products = products;
		if (this.products == null) {
			this.products = new ArrayList<Product>();
		}
	}

	@Override
	public int getCount() {
		return products.size();
	}

	@Override
	public Product getItem(int position) {
		return products.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_product_list, null);
			holder = new ViewHolder();
			holder.iconImageView = (ImageView) convertView
					.findViewById(R.id.img_product_icon);
			holder.titleTextView = (TextView) convertView
					.findViewById(R.id.txt_pro_title);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Product product = getItem(position);
		imageLoader
				.displayImage(product.getProductLogo(), holder.iconImageView);
		holder.titleTextView.setText(product.getProductTitle());
		return convertView;
	}

	private final class ViewHolder {
		ImageView iconImageView;
		TextView titleTextView;
		TextView priceTextView;
	}

}
